package com.dev.widget.ui;

import com.dev.utils.LogUtils;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Path;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.media.image.PixelMap;
import ohos.multimodalinput.event.TouchEvent;

public class SignView extends Component implements  Component.TouchEventListener{

    // 绘制路径
    private Path  mPath;
    // 绘制画笔
    private Paint mPaint;
    // 绘制路径 X、Y ( 临时变量 )
    private float mX, mY;
    // 是否清空画布
    private boolean        mIsClearCanvas = false;
    // 绘制回调事件
    private OnDrawCallback mCallback;

    public SignView(Context context) {
        super(context);
        initAttr();
    }

    public SignView(Context context, AttrSet attrSet) {
        super(context, attrSet);
        initAttr();
    }

    public SignView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        initAttr();
    }

    public SignView(Context context, AttrSet attrSet, int resId) {
        super(context, attrSet, resId);
        initAttr();
    }

    private void initAttr() {
        addDrawTask(drawTask);
        setTouchEventListener(this::onTouchEvent);
        // 设置画笔
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE_STYLE);
        paint.setStrokeWidth(30f);
        paint.setColor(Color.BLACK);
        setPaint(paint);
    }

    private DrawTask drawTask = new DrawTask() {
        @Override
        public void onDraw(Component component, Canvas canvas) {
            Path  path  = getPath();
            Paint paint = getPaint();
            // 触发回调
            if (mCallback != null) {
                if (mCallback.onDraw(canvas, path, paint, mIsClearCanvas)) {
                    // 绘制路径
                    canvas.drawPath(path, paint);
                }
                mIsClearCanvas = false;
            } else {
                // 绘制路径
                canvas.drawPath(path, paint);
                // 重置画布操作
                _resetCanvas(canvas);
            }
        }
    };



    /**
     * 重置画布处理
     * @param canvas 画布
     */
    private void _resetCanvas(Canvas canvas) {
        if (mIsClearCanvas && canvas != null) {
            canvas.drawColor(Color.WHITE.getValue(), Canvas.PorterDuffMode.CLEAR);
        }
        mIsClearCanvas = false;
    }

    // =

    /**
     * 清空画布
     * @return {@link SignView}
     */
    public SignView clearCanvas() {
        mX = mY = 0;
        mPath = null;
        mIsClearCanvas = true;
        invalidate();
        return this;
    }

    /**
     * 通过 View 绘制为 Bitmap
     * <pre>
     *     // 可以自己设置背景色
     *     CapturePictureUtils.setBackgroundColor(Color.WHITE);
     * </pre>
     * @return {@link PixelMap}
     */
    public PixelMap snapshotByView() {
       // return CapturePictureUtils.snapshotByView(this);
        return null;
    }

    /**
     * 获取绘制路径
     * @return {@link Path}
     */
    public Path  getPath() {
        if (mPath == null) mPath = new Path();
        return mPath;
    }

    /**
     * 设置绘制路径
     * @param path {@link Path}
     * @return {@link SignView}
     */
    public SignView setPath(Path path) {
        this.mPath = path;
        return this;
    }

    /**
     * 获取绘制画笔
     * @return {@link Paint}
     */
    public Paint getPaint() {
        if (mPaint == null) {
            mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setStyle(Paint.Style.STROKE_STYLE);
            mPaint.setStrokeWidth(10);
        }
        return mPaint;
    }

    /**
     * 设置绘制画笔
     * @param paint {@link Paint}
     * @return {@link SignView}
     */
    public SignView setPaint(Paint paint) {
        this.mPaint = paint;
        return this;
    }

    /**
     * 设置绘制回调事件
     * @param callback 绘制回调事件
     * @return {@link SignView}
     */
    public SignView setDrawCallback(OnDrawCallback callback) {
        this.mCallback = callback;
        return this;
    }

    private static final String TAG = SignView.class.getSimpleName();
    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        switch (touchEvent.getAction()) {
            case TouchEvent.PRIMARY_POINT_DOWN:
                mX = touchEvent.getPointerScreenPosition(touchEvent.getPointerId(0)).getX();
                mY = touchEvent.getPointerScreenPosition(touchEvent.getPointerId(0)).getY() - 250;
                getPath().moveTo(mX, mY);
                LogUtils.error(TAG, "mX = " + mX + " mY = " + mY);
                break;
            case TouchEvent.POINT_MOVE:
                float x1 = touchEvent.getPointerScreenPosition(touchEvent.getPointerId(0)).getX();
                float y1 = touchEvent.getPointerScreenPosition(touchEvent.getPointerId(0)).getY() - 250;
                LogUtils.error(TAG, "x1 = " + x1 + " y1 =" + y1);
                float cx = (x1 + mX) / 2 ;
                float cy = (y1 + mY) / 2 ;
                getPath().quadTo(mX, mY, cx, cy);
                mX = x1;
                mY = y1;
                break;
        }
        invalidate();
        return true;
    }

    /**
     * detail: 绘制回调事件
     * @author Ttt
     */
    public static abstract class OnDrawCallback {

        /**
         * 绘制方法
         * @param canvas      画布
         * @param path        绘制路径
         * @param paint       绘制画笔
         * @param clearCanvas 是否清空画布
         * @return 是否接着进行绘制 drawPath
         */
        public boolean onDraw(
                Canvas canvas,
                Path path,
                Paint paint,
                boolean clearCanvas
        ) {
            if (clearCanvas && canvas != null) {
                canvas.drawColor(Color.WHITE.getValue(), Canvas.PorterDuffMode.CLEAR);
            }
            return true;
        }
    }
    }

